package top.jiangqiang.qianyi.common.filter.caching;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import top.jiangqiang.qianyi.common.filter.AbstractPathMatchingOrderedFilter;
import top.jiangqiang.qianyi.common.filter.caching.wrapper.CachingHttpServletRequestWrapper;

import java.io.IOException;
import java.util.List;

/**
 * 对body进行缓存，后续可重复读取
 *
 * @Date 2024/6/6 下午2:05
 * @Author Jiangqiang
 */
@Slf4j
public class CachingFilter extends AbstractPathMatchingOrderedFilter {
    private final int maxCacheLimit;

    public CachingFilter(List<String> includePatterns, int order, List<String> excludePatterns, int maxCacheLimit) {
        super(includePatterns, order, excludePatterns);
        this.maxCacheLimit = maxCacheLimit;
    }

    @Override
    protected void doEnhancedFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        chain.doFilter(new CachingHttpServletRequestWrapper(request, maxCacheLimit), response);
    }
}